package com.example.neusoft.yuejian_12.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.JavaBean.Training;
import com.example.neusoft.yuejian_12.Listener.TrainingDetailListener;
import com.example.neusoft.yuejian_12.Model.TrainingModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

/**
 * Created by admin on 2017/10/17.
 */

public class TrainingDetailActivity extends AppCompatActivity {
    private Training info;
    private String id;
    private TextView tname,tintro,note,time,tprice,tplace,tphone;
    private ImageView pic;
    private TextView sign,zixun;
    private  ImageView back;
     String trname,thumb,intro,notice,trprice,place,update_time,tel;
   TrainingDetailListener trainingDetailListener=new TrainingDetailListener() {
        @Override
        public void onResponse(Training listBean) {
            info = listBean;
            initData();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainingdetail);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
      TrainingModel model = new TrainingModel();
       model.getdetail(id, trainingDetailListener);
        initViews();
        initEvents();
    }


    public void initViews() {
        tname = (TextView) findViewById(R.id.tname);
        pic = (ImageView) findViewById(R.id.tpic);
        back=(ImageView)findViewById(R.id.back);
        sign=(TextView)findViewById(R.id.tsign);
        zixun=(TextView)findViewById(R.id.tzixun);
        tintro=(TextView)findViewById(R.id.tintro);
        note=(TextView)findViewById(R.id.tnote);
        time=(TextView)findViewById(R.id.ttime);
        tprice=(TextView)findViewById(R.id.tprice);
        tplace=(TextView)findViewById(R.id.tplace);
        tphone=(TextView)findViewById(R.id.phone);
    }

    public void initEvents() {
        zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TrainingDetailActivity.this, "电话", Toast.LENGTH_SHORT).show();
                Intent phone = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tel);
                phone.setData(data);
                startActivity(phone);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(TrainingDetailActivity.this,TrainingSignActivity.class);
                intent.putExtra("train_id",info.getId());
                intent.putExtra("train_name",trname);
                intent.putExtra("train_price",trprice);
                intent.putExtra("train_notice",notice);
                startActivity(intent);
            }
        });

    }

    public void initData() {
        trname = info.getName();
        intro=info.getIntro();
        place=info.getPlace();
        trprice=info.getPrice();
        tel=info.getTel();
        update_time=info.getUpdate_time();
        notice=info.getNotice().replaceAll("</p>","").replaceAll("<p>","");
        thumb = info.getThumb();

        tname.setText(trname);
        tintro.setText(intro);
        tplace.setText(place);
        tprice.setText(trprice+"元");
        time.setText(update_time);
        note.setText(notice);
        tphone.setText(tel);
        if (thumb != "") {
            Picasso.with(TrainingDetailActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/" + thumb)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.qya);
        }
    }
}


